<script setup>
import { ref, onMounted } from 'vue'
import PageFullContainer from '@/components/pageFullContainer.vue'
import {
  getOrderService,
  changeOrderService,
  deleteOrderService,
  searchOrderService
} from '@/apis/order'
import dayjs from 'dayjs'

// 存储用户预约记录
const orderList = ref([])

// 实现分页需要的参数
const pageSize = ref(5)
const page = ref(1)
const total = ref(null)

// 获取预约记录需要的参数
const orderItem = ref({
  pageSize: pageSize.value,
  page: page.value
})

// 获取用户预约记录
const getOrder = async () => {
  isLoading.value = true
  const res = await getOrderService(orderItem.value)
  orderList.value = res.data.data.rows
  total.value = res.data.data.count
  isLoading.value = false
}

onMounted(() => {
  getOrder()
})

// 控制loading加载的开启和关闭
const isLoading = ref(false)

// 处理页码变化
const handleCurrentChange = (newPage) => {
  orderItem.value.page = newPage
  getOrder()
}

// 处理页面显示数据条数的变化
const handleSizeChange = (newSize) => {
  orderItem.value.pageSize = newSize
  getOrder()
}

// 根据后台返回的预约信息状态选择相应的标签颜色
const getTagType = (status) => {
  switch (status) {
    case '待处理':
      return 'primary'
    case '同意':
      return 'success'
    case '拒绝':
      return 'danger'
    case '已完成':
      return 'info'
    default:
      return 'other'
  }
}

// 删除预约记录
const deleteOrder = async (row) => {
  await ElMessageBox.confirm('你确定要移除这条预约记录吗？', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })

  // 删除预约记录
  await deleteOrderService(row.id)
  ElMessage.success('删除成功')
  getOrder()
}

// 预约状态选项
const statusOption = [
  { label: '待处理', command: '待处理' },
  { label: '同意', command: '同意' },
  { label: '拒绝', command: '拒绝' },
  { label: '已完成', command: '已完成' }
]

// 修改预约状态
const changeStatus = async (id, status) => {
  await changeOrderService({ id, status })
  getOrder()
  ElMessage.success('修改状态成功')
}

// 搜索框内输入的内容
const SearchInput = ref(null)

// 搜索功能
const handleSearch = async () => {
  const res = await searchOrderService(SearchInput.value)
  orderList.value = res.data.data
}

// 重置功能
const handleReset = () => {
  SearchInput.value = null
  getOrder()
}
</script>

<template>
  <PageFullContainer title="预约记录">
    <template #extra>
      <div class="extra">
        <div class="search">
          <el-input
            v-model="SearchInput"
            style="width: 240px"
            placeholder="请输入搜索内容"
            @keyup.enter="handleSearch"
            clearable
          />
          <el-button type="primary" @click="handleSearch">
            <template #default>搜索</template>
            <template #icon>
              <el-icon><i-ep-Search></i-ep-Search></el-icon>
            </template>
          </el-button>
          <el-button type="info" @click="handleReset">
            <template #default>重置</template>
            <template #icon>
              <el-icon><i-ep-Refresh></i-ep-Refresh></el-icon>
            </template>
          </el-button>
        </div>
      </div>
    </template>

    <!-- 预约记录 -->
    <el-table
      :data="orderList"
      style="width: 100%"
      v-loading="isLoading"
      v-if="orderList.length"
    >
      <el-table-column prop="date" type="index" label="序号" width="60" />
      <el-table-column label="房源名称">
        <template #default="scope">
          <div class="ellipsis">
            {{ scope.row.property.title }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="房源地址">
        <template #default="scope">
          <div class="ellipsis">
            {{ scope.row.property.address }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="房源价格">
        <template #default="scope">
          {{ Math.floor(scope.row.property.price) }}
        </template>
      </el-table-column>
      <el-table-column prop="user_name" label="预约人姓名" />
      <el-table-column prop="user_phone" label="联系电话" />
      <el-table-column prop="user_email" label="电子邮箱" />
      <el-table-column label="预约时间">
        <template #default="scope">
          {{ dayjs(scope.row.order_date).format('YYYY-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>
      <el-table-column label="预约状态">
        <template #default="scope">
          <el-tag
            effect="dark"
            :type="getTagType(scope.row.status)"
            size="large"
            round
          >
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-dropdown
            style="margin-right: 5px"
            trigger="click"
            @command="handleCommand"
          >
            <el-button type="primary" circle>
              <template #icon>
                <i-ep-List></i-ep-List>
              </template>
            </el-button>

            <!-- 折叠下拉的部分 -->
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item
                  v-for="(item, index) in statusOption"
                  :key="index"
                  :command="item.command"
                  @click="changeStatus(scope.row.id, item.command)"
                  >{{ item.label }}</el-dropdown-item
                >
              </el-dropdown-menu>
            </template>
          </el-dropdown>

          <el-button type="danger" circle @click="deleteOrder(scope.row)">
            <template #icon>
              <i-ep-Delete></i-ep-Delete>
            </template>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-empty v-else :image-size="200" />

    <template #footer>
      <!-- 分页区域 -->
      <el-pagination
        background
        layout="jumper,total,sizes,prev,pager,next"
        :page-sizes="[2, 3, 5, 10]"
        v-model:page-size="pageSize"
        v-model:current-page="page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :total="total"
      ></el-pagination>
    </template>
  </PageFullContainer>
</template>

<style lang="scss" scoped>
.extra {
  width: 1250px;
  display: flex;
  justify-content: space-between;

  .search {
    button {
      margin: 0 10px;
    }
  }
}
</style>
